import java.io.*;
import java.util.ArrayList;
import javax.swing.filechooser.FileFilter;

/**
 * Created by IntelliJ IDEA.
 * User: bo
 * Date: 3/6/12
 * Time: 2:36 PM
 * To change this template use File | Settings | File Templates.
 */
public class FileHandler {
    private static FileOutputStream fileOutputStream = null;
    private static ObjectOutputStream objectOutputStream = null;
    private static FileInputStream fileInputStream = null;
    private static ObjectInputStream objectInputStream = null;

    public static void saveQuiver(File file, Quiver quiver) {
        File writeFile = new File(file.getAbsolutePath()+".qi");

        try {
            fileOutputStream = new FileOutputStream(writeFile);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(quiver);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
            try{
                objectOutputStream.flush();
                objectOutputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveQuiverSession(File file, ArrayList<Quiver> quivers) {
        File writeFile = new File(file.getAbsolutePath()+".qis");

        try {
            fileOutputStream = new FileOutputStream(writeFile);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(quivers);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
            try{
                objectOutputStream.flush();
                objectOutputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static ArrayList<Quiver> openQuiverSession(File file) {
        ArrayList<Quiver> quivers = null;
        try {
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);
            quivers = (ArrayList<Quiver>) objectInputStream.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                objectInputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return quivers;
        //TODO - find more spesific exception than IOException, e.g corruptstream and invalidcast or sumtn.
        //TODO - display message to user of relevant caught exceptions.
    }

    public static Quiver openQuiver(File selectedFile) {
        Quiver quiver = null;
        try {
            fileInputStream = new FileInputStream(selectedFile);
            objectInputStream = new ObjectInputStream(fileInputStream);
            quiver = (Quiver) objectInputStream.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                objectInputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return quiver;
    }
}

class QuiverFilter extends FileFilter {
    public boolean accept(File file) {
        return file.isDirectory() || file.getName().toLowerCase().endsWith(".qi");
    }
    public String getDescription() {
        return ".qi Quiver files";
    }
}

class QuiverSessionFilter extends FileFilter {
    public boolean accept(File file) {
        return file.isDirectory() || file.getName().toLowerCase().endsWith(".qis");
    }
    public String getDescription() {
        return ".qis Quiver session files";
    }
}

